package ext;

import java.util.concurrent.locks.*;

public class ParaPrint {
    
    final static Lock lock = new ReentrantLock();
    final static Condition alpha = lock.newCondition(),
                    num = lock.newCondition();
    final static int n = 10;
                    
    public static void printNum() {
        for (int i = 0; i < n; i++) {
            lock.lock();
            try {
                if (i > 0) {
                    num.await();
                }
                System.out.println(i);
                alpha.signalAll();
            } catch (Exception e) {
            } finally {
                lock.unlock();
            }
        }
    }

    public static void printAlpha() {
        for (int i = 0; i < n; i++) {
            lock.lock();
            try {
                alpha.await();
                System.out.println((char)(i + 'A'));
                num.signalAll();
            } catch (Exception e) {
            } finally {
                lock.unlock();
            }
        }
    }

    public static void main(String[] args) {
        new Thread(() -> {
            printAlpha();
        }).start();
        new Thread(() -> {
            printNum();
        }).start();
    }

}
